﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class DangNhap : Form
    {
        public string taiKhoan;
        private string matKhau;

        public DangNhap()
        {
            InitializeComponent();
        }

        public DangNhap(string TK, string MK)
        {
            InitializeComponent();
            this.taiKhoan = TK;
            this.matKhau = MK;
        }

        private bool dangnhap(string TK, string MK)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from NhanVien where TenDangNhap_NV = @TenDangNhap_NV";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@TenDangNhap_NV", txtTaiKhoan.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                if ((string)reader["MatKhau_NV"] == txtMatKhau.Text)
                {
                    taiKhoan = TK;
                    matKhau = MK;
                    reader.Close();
                    conn.Close();
                    return true;
                }
                else
                {
                    reader.Close();
                    LblLoiMatKhau.Text = "Mật khẩu không đúng";
                    conn.Close();
                    return false;
                }
            }
            else
            {
                reader.Close();
                LblLoiTaiKhoan.Text = "Tài khoản không đúng";
                conn.Close();
                return false;
            }
            
            
        }

        private void DangNhapButton_Click(object sender, EventArgs e)
        {
            if (dangnhap(txtTaiKhoan.Text, txtMatKhau.Text) == true)
            {
                this.DialogResult = DialogResult.OK;
                this.taiKhoan = txtTaiKhoan.Text;
                this.Close();
            }
        }

        private void ThoatButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
